// packageC/pages/takeOutIndex/takeOutIndex.js
const app = getApp()
const common = require('../../../public.js')
Page({

    /**
     *   页面的初始数据
     */
    data: {
        needList:[],
        eatImage:[],
        eatNums:'',
        eatNumsFlag:'',
        seat:'',
        storeName:'',
        storeId:'8637',
        TableId:'',
        deskName:'',
        selectA:true,
        selectB:true,
        recordFixed:false,
        currentNum:null,
        currentSelectNum:null,
        lun: true,
        // topImgList:[{"id":19,"regionId":1,"imgUrl":"http://feiyangimage.oss-cn-shanghai.aliyuncs.com/ad_storeListTop/29/1646874448125064.png","redirectPath":"","redirectUrl":"","createTime":1646874452000,"sort":null,"imgType":1,"videoPic":"","module":2,"storeType":29,"remark":"","redirectZfb":null,"redirectYsf":null,"redirectOth1":null,"redirectOth2":null,"managerId":null,"platformId":null,"cardId":null,"status":null}],
        // numList:[{id:1},{id:2},{id:3},{id:4},{id:5},{id:6},{id:7},{id:8},{id:9},{id:10},{id:11},{id:12},{id:13},{id:14},{id:15},{id:16},{id:17},{id:18}],
        numList:[],
     
    },
    selectAFunc(){
  
          var that = this;
          console.log('tt---',that.data.selectA);
          that.setData({
            //  selectA:that.data.selectA==true?false:true
        })
      },

selectBFunc(){
  
              var that = this;
              console.log('tt---',that.data.selectB);
              that.setData({
                //   selectB:that.data.selectB==true?false:true
            })
          },
    numClick(e){
        let currentIndex = e.currentTarget.dataset.index
        console.log(currentIndex);
        if(currentIndex==11&&this.data.recordFixed==false){
            this.setData({
                recordFixed:true
            })
            return;
        }
        // if(currentIndex==11){
        //     this.setData({
        //         currentNum:this.data.currentNum==11?null:currentIndex,
        //     })
        // }else{
            this.setData({
                currentNum:currentIndex,
                currentSelectNum: currentIndex+1,
            })
        // }
       
       
    },
  

    // 时间戳
    time(time) {
        return common.dateFormat('yyyy-MM-dd HH:mm', time)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('就餐人数页面--',options);
        let screenHeight = wx.getSystemInfoSync().windowHeight
        let okHeight = (screenHeight - 88 - 110 - 80 - 65)
        const that = this
        this.setData({
            toView: 'a1',
        })
        // 返回按钮
        let capsule = wx.getMenuButtonBoundingClientRect()
        this.setData({
            okHeight,
            height: capsule.height,
            top: capsule.top,
            storeId: options.id ? options.id : 8637,
        })
        console.log('aaaaaa-----');

        if (options.q) {
            console.log('bbbbbb-----');
            let scan_url = decodeURIComponent(options.q);
            console.log("进来0")
  
            let pos1 = scan_url.indexOf("storeId="); // 
            let pos2 = scan_url.indexOf("&TableId="); // 
            let pos3 = scan_url.indexOf("&deskName="); // 
            let pos4 = scan_url.indexOf("&seat="); // 
            

            var storeId = scan_url.substring(pos1+8, pos2)
            var TableId = scan_url.substring(pos2+9, pos3)
            var deskName = scan_url.substring(pos3+10, pos4)
            var seat = scan_url.substring(pos4+6, scan_url.length)
 
            options.storeId = storeId;
            options.TableId = TableId;
            options.deskName = deskName;
            options.seat = seat;
            console.log('赋值后--storeId--',options.storeId,'---TableId',options.TableId,'---deskName',options.deskName,'---seat',options.seat);
            var list = [];
            for(var i = 0;i < parseInt(options.seat);i++){
                var json = {};
                json.id = i+1;
                list.push(json);
            }
console.log('list=--',list);
            // this.data.storeId = options.storeId
            // this.data.TableId = options.TableId
            // this.data.deskName = options.deskName
            that.setData({
                storeId:options.storeId,
                TableId: options.TableId,
                deskName:options.deskName,
                seat:options.seat,
                numList:list,
            })
            common.getLocalUserInfo((success, data) => {
                if (success) {

                    that.setData({
                        userInfo: data.user,
                        token: data.token,
                        openid: data.openid,
                    }, () => {
                        that.getStoreInfo(options.storeId)
                        that.mainPageInfoFunc(options.storeId);
                        that.getNeedList(options.storeId);
                    })

                }

                
            }, true)
            return
            //  公眾號
        }
        console.log('xihha--',options.seat);
        var list = [];
        for(var i = 0;i < parseInt(options.seat);i++){
            var json = {};
            json.id = i+1;
            list.push(json);
        }
console.log('list=--',list);
        that.setData({
            storeId:options.storeId,
            TableId: options.TableId,
            deskName:options.deskName,
            numList:list,

        })

        // 用户信息
        common.getLocalUserInfo((success, data) => {
            if (success) {
                this.setData({
                    userInfo: data.user,
                    token: data.token,
                    openid: data.openid,
                }, () => {
                    that.getStoreInfo('8637');
                    that.mainPageInfoFunc('8637');
                    that.getNeedList('8637');
                    // this.clickCollect()  //是否收藏
                    // this.gong(true)
                    // this.queryCollection()
                    // this.getCartNum(true)
                    // that.getStoreCoupons(that.data.storeId, true)
                  
                })
            }
        }, true)

        // 定位
        // that.getLocation()
    },
   
    // 获取当前定位
    getLocation() {
        
        app.getLocation(
            (data) => {
                this.setData({
                    lon: data.longitude,
                    lat: data.latitude
                }, () => {
                    this.shopInfo()
                    this.getHasSelectShopItemIdAndCountFunc();
         
                    // this.getLeftBadgeNum(true)
                   
                })
            },
            (err) => {
                // 定位失败回调
                this.setData({
                    refreshing: false
                })
            })
    },

    // 返回上页面
    goBack() {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
        if (prevPage) {
            wx.navigateBack({
                delta: 1
            })
        } else {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        }
    },

    judgeSelectPersonFunc(storeId) {
        var that = this;
      
        console.log('xxxx-');
        const param = {
            storeId:this.data.storeId,

            storeDeskId:this.data.TableId,
            // employeeId: this.data.employeeUserId,
            // employeeId: "",
            userId: this.data.userInfo.id,
        }
        console.log('jjj',param);
        common.post('/store/getEatNumsFlag',
            param,
            res => {
                console.log('sss信息为--',res);
                if (res.status === 200) {
                   if(res.data!='0'){// 已选择
                    //  common.showToast('当前有人已选择就餐人数,请勿重复选择~')
                     wx.showToast({
                        title: '当前有人已选择就餐人数,为您自动跳转~',
                        icon: 'none',
                        duration: 3000,
                        complete:() => {
                            that.toMealList(res.data);
                        }});
                     
                   }else{
                     that.saveEatNumsFunc();
                   }

                }
            },
            () => {
                console.log('dsadsdsd');
               
            })
    },
    saveEatNumsFunc(storeId) {
        var that = this;
      
        console.log('xxxx-');
        const param = {
            storeId,
            eatNums:this.data.currentSelectNum,
            storeDeskId:this.data.TableId,
            // employeeId: this.data.employeeUserId,
            // employeeId: "",
            userId: this.data.userInfo.id,
        }
        console.log('jjj',param);
        common.post('/store/saveEatNums',
            param,
            res => {
                console.log('信息为--',res);
                if (res.status === 200) {
                    that.toMealList(this.data.currentSelectNum);

                }
            },
            () => {
                console.log('dsadsdsd');
               
            })
    },
    getStoreInfo(storeId) {
        var that = this;
        this.setData({
            loading: true
        })
        console.log('xxxx-');
        const param = {
            storeId,
            // employeeId: this.data.employeeUserId,
            employeeId: "",
            userId: this.data.userInfo.id,
        }
        console.log('jjj',param);
        common.post('/store/getStoreAndBranchInfo',
            param,
            res => {
                console.log('信息为--',res);
                if (res.status === 200) {
                    this.setData({
        
                        storeName: res.data.name,
       
                    }, function () {
                      
                      })

                }
            },
            () => {
                console.log('dsadsdsd');
               
            })
    },
    sureBtn(){
if(this.data.currentNum==null){
 return   common.showToast('请选择就餐人数~')
}
this.judgeSelectPersonFunc();
    },
    toMealList(selectPerson){
        var that = this
        var tempJson = {
            storeId:this.data.storeId,
       
            
            // xpoint: this.data.lat,
            // ypoint: this.data.lon,
            xcFlag: '0' //是否含有携程门票 0不含有
        }
        common.get('/store/mainPageInfo', tempJson, res => {
            that.setData({
               
            }, () => {
                console.log('url---++===',`/packageC/pages/scanMeal/scanMeal?id=${that.data.storeId}&tableId=${that.data.TableId}&deskName=${that.data.deskName}&name=${that.data.storeName}&currentSelectNum=${selectPerson}`);
                wx.navigateTo({
                    url: `/packageC/pages/scanMeal/scanMeal?id=${that.data.storeId}&tableId=${that.data.TableId}&deskName=${that.data.deskName}&name=${that.data.storeName}&currentSelectNum=${selectPerson}`,
                })

            })
            console.log(res.data.store, "75456666665")
        })
    },


    mainPageInfoFunc(storeId){
        var that = this
        var tempJson = {
            storeId:storeId,

            xcFlag: '0' //是否含有携程门票 0不含有
        }
        common.get('/store/mainPageInfo', tempJson, res => {
            console.log('1111---mainPageInfo---',res);
            var tempList = [];
            if(res.data.store.eatImage){
                var json = {imgUrl:res.data.store.eatImage};
                tempList.push(json);
            }
            that.setData({
                eatImage:tempList,
                eatNums:res.data.store.eatNums,
                eatNumsFlag:res.data.store.eatNumsFlag,
            }, () => {
                if(that.data.eatNumsFlag!='1'){
                    that.toMealList();
                }


            })
            console.log(res.data.store, "75456666665")
        })
    },

    getNeedList(storeId){
        var that = this
        var tempJson = {
            storeId:storeId,

        }
        common.get('/store/getEatNeedGoodsList', tempJson, res => {
            console.log('2222---getEatNeedGoodsList---',res);
            that.setData({
                needList:res.data
            })
        })
    },



})